//
//  CCHud.m
//  CCKit
//
//  Created by tucici on 2019/4/12.
//  Copyright © 2019 tucici. All rights reserved.
//

#import "CCHud.h"
#import "CCLayoutConstraint.h"
#import "NSString+category_CCKit.h"
@interface CCHud ()
@property (nonatomic, strong) UIActivityIndicatorView *hudView;
@property (nonatomic, strong) UILabel *label;
@end
@implementation CCHud
static CCHud * _cchud;
static dispatch_once_t hudtoken;
+ (instancetype)hud
{
    if (!_cchud)
    {
        dispatch_once(&hudtoken, ^{
            _cchud = [[CCHud alloc] init];
        });
    }
    return _cchud;
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.bounds = CGRectMake(0, 0, 100, 100);
        self.layer.cornerRadius = 5.0;
        self.layer.masksToBounds = YES;
        self.alpha = 1;
        self.times = 1.3;
        self.insetLR = 20.0;
        self.backgroundColor = [UIColor blackColor];
        self.windowLevel = UIWindowLevelAlert;
        [self makeKeyAndVisible];
    }
    return self;
}
- (UIActivityIndicatorView *)hudView
{
    if (!_hudView)
    {
        _hudView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:0];
        _hudView.bounds = CGRectMake(0, 0, self.bounds.size.width / 2 , self.bounds.size.width / 2);
        _hudView.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:_hudView];
        [self setupHudViewLayout];
    }
    return _hudView;
}
- (UILabel *)label
{
    if (!_label)
    {
        _label = [[UILabel alloc]init];
        _label.backgroundColor = [UIColor clearColor];
        _label.numberOfLines = 0;
        _label.textColor = [UIColor whiteColor];
        _label.textAlignment = NSTextAlignmentCenter;
        _label.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        _label.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:_label];
        [self setupLabelLayout];
    }
    return _label;
}

- (void)setColor:(UIColor *)color
{
    if (_color != color)
    {
        _color = color;
        self.hudView.color = color;
        self.label.textColor = color;
    }
}

- (void)setTimes:(CGFloat)times
{
    if (_times != times)
    {
        _times = times;
        
        if (self.hudView.bounds.size.width * times <= self.bounds.size.width )
        {
            self.hudView.transform = CGAffineTransformMakeScale(times, times);
        }
    }
}

- (void)setBounds:(CGRect)bounds
{
    [super setBounds:bounds];
    for (NSLayoutConstraint * constraint in [[self constraints] reverseObjectEnumerator])
    {
        if ((constraint.firstAttribute == CCWidth || constraint.firstAttribute == CCHeight) && [constraint.firstItem isEqual:self])
            [self removeConstraint:constraint];
    }
    CCLayoutConstraint * w = [CCLayoutConstraint a:self attr:CCWidth r:CCEqual b:nil attr:CCNotAttribute m:1 c:self.bounds.size.width];
    CCLayoutConstraint * h  = [CCLayoutConstraint a:self attr:CCHeight r:CCEqual b:nil attr:CCNotAttribute m:1 c:self.bounds.size.height];
    [self addConstraints:@[w,h]];
}

- (void)setupHudViewLayout
{
    CCLayoutConstraint * centerX  = [CCLayoutConstraint a:self.hudView attr:CCCenterX r:CCEqual b:self attr:CCCenterX m:1 c:0];
    CCLayoutConstraint * centerY = [CCLayoutConstraint a:self.hudView attr:CCCenterY r:CCEqual b:self attr:CCCenterY m:1 c:0];
    CCLayoutConstraint * width = [CCLayoutConstraint a:self.hudView attr:CCWidth r:CCEqual b:nil attr:CCNotAttribute m:1 c:50];
    CCLayoutConstraint * height = [CCLayoutConstraint a:self.hudView attr:CCHeight r:CCEqual b:nil attr:CCNotAttribute m:1 c:50];
    
    [_hudView addConstraints:@[width, height]];
    [self addConstraints:@[centerX,centerY]];
}
- (void)setupLabelLayout
{
    CCLayoutConstraint * top = [CCLayoutConstraint a:self.label attr:CCTop r:CCEqual b:self.hudView attr:CCBottom m:1 c:0];
    CCLayoutConstraint * left = [CCLayoutConstraint a:self.label attr:CCLeft r:CCEqual b:self attr:CCLeft m:1 c:0];
    CCLayoutConstraint * right = [CCLayoutConstraint a:self.label attr:CCRight r:CCEqual b:self attr:CCRight m:1 c:0];
    CCLayoutConstraint * bottom = [CCLayoutConstraint a:self.label attr:CCBottom r:CCEqual b:self attr:CCBottom m:1 c:0];
    [self addConstraints:@[top,left,right,bottom]];
}

- (void)updateHudViewLayout
{
    self.bounds = CGRectMake(0, 0, 100, 100);//hud的bounds初始化
    CGSize size = [self.label.text sizeWithFont:self.label.font inWidth:self.bounds.size.width];
    [self autoZoom:size];//根据title自动锁放Hud
    
    for (NSLayoutConstraint * constraint in [[self constraints] reverseObjectEnumerator])
    {
        if ((constraint.firstAttribute == CCTop || constraint.firstAttribute == CCCenterY) && [constraint.firstItem isEqual:self.hudView])
            [self removeConstraint:constraint];
    }
    
    CCLayoutConstraint *newContraint;
    if ([CCHud hud].label.hidden == YES)
        newContraint = [CCLayoutConstraint a:self.hudView attr:CCCenterY r:CCEqual b:self attr:CCCenterY m:1 c:0];
    else
        newContraint = [CCLayoutConstraint a:self.hudView attr:CCTop r:CCEqual b:self attr:CCTop m:1 c:10];
    [self addConstraint:newContraint];
}

//根据title自动锁放Hud
- (void)autoZoom:(CGSize)size
{
    if (size.width <= 0 || size.height <=  0) {
        return;
    }
    CGRect bounds = [[UIScreen mainScreen] bounds];
    ///优先往width方向扩展
    if (size.width > self.bounds.size.width - 50 && (bounds.size.width - self.insetLR * 2 >= self.bounds.size.width + 18)){
        self.bounds = CGRectMake(0, 0, self.bounds.size.width + 18, self.bounds.size.height);
        CGSize size = [self.label.text sizeWithFont:self.label.font inWidth:self.bounds.size.width];
        [self autoZoom:size];
    }
    ///width扩展够了 ，再往height扩展
    else if (size.height > self.bounds.size.height - 50){
        self.bounds = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height + 20);
        CGSize size = [self.label.text sizeWithFont:self.label.font inWidth:self.bounds.size.width];
        [self autoZoom:size];
    }
}
+ (void)startHud
{
    [CCHud hud].hidden = NO;
    [[CCHud hud].hudView startAnimating];
    [CCHud hud].label.hidden = YES;
    [CCHud hud].label.text = @"";
    [[CCHud hud] updateHudViewLayout];
}

+ (void)stopHud
{
    [CCHud hud].hidden = YES;
    [[CCHud hud].hudView stopAnimating];
    [CCHud hud].label.hidden = YES;
    [CCHud hud].label.text = @"";
}

+ (void)startHud:(NSString *)title
{
    [CCHud hud].hidden = NO;
    [[CCHud hud].hudView startAnimating];
    [CCHud hud].label.hidden = title.length <= 0;
    [CCHud hud].label.text = NSLocalizedString(title, comment: "");
    [[CCHud hud] updateHudViewLayout];
}
+ (void)startHud:(NSString *)title stopTime:(CGFloat)time handle:(nullable void (^)(void))block
{
    //    if (!title)
    //        [self stopHud:block];
    //    else if([title length] <= 0)
    //        [self stopHud:block];
    //    else
    if(time <= 0)
        [self stopHud:block];
    else
    {
        [self startHud:title];
        [self performSelector:@selector(stopHud:) withObject:block afterDelay:time];
    }
}
+ (void)stopHud:(nullable void(^) (void))block
{
    [self stopHud];
    [[CCHud hud].hudView isAnimating];
    if (block)
    {
        block();
    }
}
+ (void)msg:(NSString *)msg{
    [self startHud:msg stopTime:1 handle:nil];
}
+ (void)level:(UIWindowLevel)l{
    CCHud.hud.windowLevel = l;
}
@end
